package com.qingxin.wuye.repertory.service;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.wuye.repertory.entity.ResManage;

@Service
public class ResManageService extends BaseService<ResManage, String>{
	
	@SuppressWarnings("unchecked")
	public Pager listResdata(String search,String typeid,PageParam pageParam,LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.ResManage(" +
				"r.resid, r.typeid, r.resDm, r.resTm, r.resName, r.pinyin, r.model, r.unit," +
				" r.sccj, r.remark, r.stockMax, r.stockMin, r.inPrice, r.outPrice, r.middlePrice,t.typeName)" +
				" from ResManage r, ResType t" +
				" where r.typeid = t.typeid ");
		if (StringUtils.isNotEmpty(search)) {
			querier.append(" and r.resName like:resName ");
			search = search.replace("'", "");
			querier.setParam("resName", "%"+search+"%");
		}
		if (StringUtils.isNotEmpty(typeid)) {
			querier.append(" and r.typeid = :typeid ");
			querier.setParam("typeid", typeid);
		}
		
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		
		List<ResManage> list = (List<ResManage>) pager.getList();
		pager.setList(list);
		return pager;
	} 
	
	
	@SuppressWarnings("unchecked")
	public Pager listResdataHas(String search,String typeid,String repertoryid,PageParam pageParam,LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.ResManage(" +
				"r.resid, r.typeid, r.resDm, r.resTm, r.resName, r.pinyin, r.model, r.unit," +
				" r.sccj, r.remark, r.stockMax, r.stockMin, r.inPrice, r.outPrice, r.middlePrice,t.typeName)" +
				" from ResManage r , ResType t where r.typeid = t.typeid ");
				

		if (StringUtils.isNotEmpty(repertoryid)) {
			querier.append(" and r.resid in (select g.resid from Gi g where g.repertoryid=:repertoryid) ");
			querier.setParam("repertoryid",repertoryid);
		}
		if (StringUtils.isNotEmpty(typeid)) {
			querier.append(" and r.typeid = :typeid ");
			querier.setParam("typeid", typeid);
		}
		if (StringUtils.isNotEmpty(search)) {
			querier.append(" and r.resName like:resName ");
			search = search.replace("'", "");
			querier.setParam("resName", "%"+search+"%");
		}
		
		
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		
		List<ResManage> list = (List<ResManage>) pager.getList();
		pager.setList(list);
		return pager;
	} 
	
	
	/***
	 * 后台列表数据
	 * @param searchname
	 * @param typeid
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String searchname,String typeid, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("select new com.qingxin.wuye.repertory.entity.ResManage(" +
				"r.resid, r.typeid, r.resDm, r.resTm, r.resName, r.pinyin, r.model, r.unit," +
				" r.sccj, r.remark, r.stockMax, r.stockMin, r.inPrice, r.outPrice, r.middlePrice,t.typeName)" +
				" from ResManage r, ResType t" +
				" where r.typeid = t.typeid ");
		if(StringUtils.isNotEmpty(searchname)){
			searchname = searchname.replace("'", "");
			querier.append(" and r.resName like :resName ");
			querier.setParam("resName", "%"+searchname+"%");
		}
		if (StringUtils.isNotEmpty(typeid)) {
			querier.append(" and r.typeid = :typeid ");
			querier.setParam("typeid", typeid);
		}
		querier.append(pageParam.getOrderby(" order by r.createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
		
	}
	
	/**
	 * 添加类别
	 * @param resType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ResManage resManage, LoginUser loginUser, HttpServletRequest request){
		resManage.setCreateTime(new Date());
		resManage.setCreateBy(loginUser.getLogin_name());
		this.save(resManage);
		return null;
	}
	
	/**
	 * 更新
	 * @param resType
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ResManage resnManage, LoginUser loginUser, HttpServletRequest request){
		ResManage old = getSafeEntity(resnManage.getResid());
		String fields = "typeid,resDm,resTm,resName,pinyin,model,unit,sccj,remark,stockMax,stockMin,inPrice,outPrice,middlePrice";
		BeanUtil.setTargetFields(old, resnManage, fields, true);
		Updater<ResManage> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
	/**
	 * 删除
	 * @param typeids
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String[] resisd, LoginUser loginUser, HttpServletRequest request){
		for(String resid : resisd){
			ResManage resManage = get(resid);
			if(resManage==null){
				continue;
			}
			this.delete(resManage);
		}
		
		return null;
	}
}
